<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .page {
        height: 200px;
        border: 1px solid #000;
      }

      .active {
        color: red;
      }
    </style>
  </head>
  <body>
    <!-- 
      动态组件 component
        概念：通过给内置的 component 元素，动态绑定 is 属性来实现组件动态切换的效果

        语法：
          <component is="AAA"></component>      使用 AAA 组件覆盖 component
          <component is="BBB"></component>      使用 BBB 组件覆盖 component

     -->

    <div id="app">
      <button
        v-for="tab in tabs"
        :key="tab.name"
        :class="{active: tab.name === curTabName}"
        @click="curTabName = tab.name"
      >
        {{ tab.label }}
      </button>

      <hr />
      <!-- 动态渲染组件：方案一，手动通过 v-if 来控制 -->
      <!-- <home v-if="curTabName === 'Home'"></home>
      <category v-if="curTabName === 'Category'"></category>
      <cart v-if="curTabName === 'Cart'"></cart> -->

      <component :is="curTabName"></component>
    </div>

    <script src="../lib/vue.js"></script>
    <script>
      Vue.component("Home", {
        template: `
          <div class="page">Home</div>
        `,
      });

      Vue.component("Category", {
        template: `
          <div class="page">Category</div>
        `,
      });

      Vue.component("Cart", {
        template: `
          <div class="page">Cart</div>
        `,
      });

      const vm = new Vue({
        el: "#app",

        data: {
          tabs: [
            {
              label: "首页",
              name: "Home",
            },
            {
              label: "分类页",
              name: "Category",
            },
            {
              label: "购物车",
              name: "Cart",
            },
          ],

          curTabName: "Home", // 表示当前激活的Tab的name
        },
      });
    </script>
  </body>
</html>
